package com.hemaapp.yjnh.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

import com.hemaapp.hm_FrameWork.HemaAdapter;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.activity.RouteMapActivity;
import com.hemaapp.yjnh.bean.Route;

public class RouteListAdapter extends HemaAdapter implements OnClickListener {

	private Context mContext;
	private ArrayList<Route> routes;

	public RouteListAdapter(Context mContext, ArrayList<Route> routes) {
		super(mContext);
		this.mContext = mContext;
		if (routes == null)
			this.routes = new ArrayList<Route>();
		else
			this.routes = routes;
	}

	public void setroutes(ArrayList<Route> routes) {
		if (routes == null)
			this.routes = new ArrayList<Route>();
		else
			this.routes = routes;
	}

	@Override
	public int getCount() {
		int size = routes.size();
		return size == 0 ? 1 : size;
	}

	@Override
	public boolean isEmpty() {
		int size = routes.size();
		return size == 0;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (isEmpty())
			return getEmptyView(parent);

		ViewHolder holder = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.listitem_route, null);
			holder = new ViewHolder();
			holder.index = (TextView) convertView.findViewById(R.id.index);
			holder.route_txt = (TextView) convertView
					.findViewById(R.id.route_txt);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		Route route = routes.get(position);

		int indext = position + 1;
		String index_s;
		if (indext < 10) {
			index_s = "0" + String.valueOf(indext);
		} else {
			index_s = String.valueOf(indext);
		}

		holder.index.setText(index_s);
		holder.route_txt.setText(route.getName());

		convertView.setTag(R.id.TAG, route);
		convertView.setOnClickListener(this);
		return convertView;
	}

	class ViewHolder {
		TextView index;
		TextView route_txt;
	}

	@Override
	public void onClick(View v) {
		Route route = (Route) v.getTag(R.id.TAG);

		Intent it = new Intent(mContext, RouteMapActivity.class);
		it.putExtra("route", route);
		mContext.startActivity(it);

		log_i("route=" + route);
	}
}
